package cn.edu.neusoft.zengweidong707.neufood.activity;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.List;

import cn.edu.neusoft.zengweidong707.neufood.R;
import cn.edu.neusoft.zengweidong707.neufood.adapter.FoodListAdapter;
import cn.edu.neusoft.zengweidong707.neufood.bean.FoodListBean;
import cn.edu.neusoft.zengweidong707.neufood.bean.IsCollectedBean;
import cn.edu.neusoft.zengweidong707.neufood.bean.IsSuccessBean;
import cn.edu.neusoft.zengweidong707.neufood.listener.ListListener;
import cn.edu.neusoft.zengweidong707.neufood.listener.ZwdListener;
import cn.edu.neusoft.zengweidong707.neufood.model.UserModel;

public class FoodListActivity extends BaseActivity {
    private TextView shopname;
    private ImageButton back;
    private ImageButton collect;
    private UserModel model;
    private RecyclerView recyclerView;
    private List<FoodListBean> list;
    private ListListener<FoodListBean> listListener;
    private ZwdListener<IsSuccessBean> collectListener;
    private ZwdListener<IsCollectedBean> isCollectedListener;
    private FoodListAdapter foodListAdapter;
    private LinearLayoutManager linearLayoutManager;
    private String flag;
    private String success;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_food_list);
        recyclerView = (RecyclerView)findViewById(R.id.foodlist_rec);
        linearLayoutManager = new LinearLayoutManager(FoodListActivity.this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        shopname = (TextView)findViewById(R.id.shopname);
        back = (ImageButton)findViewById(R.id.back);
        collect = (ImageButton)findViewById(R.id.collect);
    }

    @Override
    void initEvent() {
        listListener = new ListListener<FoodListBean>() {
            @Override
            public void onResponse(List<FoodListBean> foodListBeans) {
                list = foodListBeans;
                foodListAdapter = new FoodListAdapter(FoodListActivity.this, list);
                recyclerView.setAdapter(foodListAdapter);
            }

            @Override
            public void onFail(String msg) {
                showToast(msg);
            }
        };

        isCollectedListener = new ZwdListener<IsCollectedBean>() {
            @Override
            public void onResponse(IsCollectedBean l) {
                flag = l.getCollected();
                if("1".equals(flag)){
                    collect.setImageResource(R.drawable.ic_collect_press);
                } else {
                    collect.setImageResource(R.drawable.ic_collect_normal);
                }
             }

            @Override
            public void onFail(String msg) {
                showToast("Failed");
            }
        };

        collectListener = new ZwdListener<IsSuccessBean>() {
            @Override
            public void onResponse(IsSuccessBean l) {
                success = l.getSuccess();
                if ("1".equals(success)) {

                    if ("1".equals(flag)) {
                        collect.setImageResource(R.drawable.ic_collect_normal);
                        flag = "0";
                        showToast("取消收藏");
                    } else {
                        collect.setImageResource(R.drawable.ic_collect_press);
                        flag = "1";
                        showToast("收藏成功");
                    }

                } else {
                    showToast("收藏失败");
                }
            }

            @Override
            public void onFail(String msg) {
                showToast("Failed");
            }
        };

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = getIntent().getExtras();
                int shopid = bundle.getInt("shopid");
                shopname.setText(bundle.getString("shopname"));
                model = new UserModel();
                int userid = Integer.parseInt(getUserid().toString());
                model.userCollectionShop(userid, shopid, collectListener);


            }
        });
    }

    @Override
    void initData() {
        Bundle bundle = getIntent().getExtras();
        int shopid = bundle.getInt("shopid");
        int userid = Integer.parseInt(getUserid().toString());
        shopname.setText(bundle.getString("shopname"));
        model = new UserModel();
        model.getFoodList(listListener, shopid);
        model.shopCollectionFlag(userid, shopid, 0, isCollectedListener);
    }

}
